#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
#define ElemType int
#define Status int
typedef struct LNode{
    ElemType data;
    struct LNode *next;
}LNode, *LinkList;
//初始化
LinkList InitList(){
    LNode *L = (LNode *)malloc(sizeof(LNode));
    if(L == NULL){
        return NULL;
    }
    L->next = NULL;
    return L;
}
//插入
Status ListInsert(LinkList L, int i, ElemType e){
    if(L == NULL){
        return ERROR;
    }
    if(i<1){
        return ERROR;
    }
    LNode *p = L;
    int j = 0;
    while(p!=NULL && j<i-1){
        p = p ->next;
        j++;
    }
    if(p == NULL){
        return ERROR;
    }
    LNode* s = (LNode*)malloc(sizeof(LNode));
    if(s==NULL){
        return ERROR;
    }
    s->data = e;
    s->next = p->next;
    p->next = s;
    return OK;
}
//删除等于val的元素
Status LinkListDelete(LinkList L,  ElemType val){
    if(L == NULL){
        return ERROR;
    }
    LNode *p = L;
    LNode *q = p->next;
    while(q!=NULL){
        if(q->data==val){
            p->next = q->next;
            free(q);
            q = p->next;
        }
        else{
            p = p->next;
            q = q->next;
        }
    }
    return OK;
}
LinkList CenterList(LinkList L){
    if(L==NULL){
        return ERROR;
    }
    LNode* p = L->next;
    LNode* q = L->next;
    while(q!=NULL &&q->next!=NULL){
        p = p->next;
        q = q->next ->next;
    }
    return p;
}
LinkList ReturnKList(LinkList L,int k){
    if(L==NULL){
        return ERROR;
    }
    LNode* p = L->next;
    LNode* q = L->next;
    int i = 0;
    while(q!=NULL && i<k){
        q = q->next;
        i++;
    }
    while(q!=NULL){
        q = q->next;
        p = p->next;
        
    }

    return p;
}
//打印
void PrintList(LinkList L){
    if(L==NULL){
        return;
    }
    LNode *p =L->next;
    while(p!=NULL){
        printf("%d\t",(p->data));
        p = p->next;
    }
    printf("\n");
}
int main(){
    LinkList L = InitList();
    ListInsert(L,1,1);
    ListInsert(L,2,2);
    ListInsert(L,3,3);
    ListInsert(L,4,4);
    ListInsert(L,5,5);
    ListInsert(L,6,6);
    ListInsert(L,7,3);
    PrintList(L);
    LNode* p = CenterList(L);
    if(p!=NULL){
        printf("%d\n",p->data);
    }
    LNode* q = ReturnKList(L,3);
    if(q!=NULL){
        printf("%d\n",q->data);
    }
    return 0;
}